package com.yc.sole.system.controller;

import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import com.yc.sole.system.biz.DeptBiz;
import com.yc.sole.system.request.DeptReq;
import com.yc.sole.system.response.DeptResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 部门管理
 *
 * @author yizuomin
 * @date 2023-03-26
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class DeptController {

    private final DeptBiz deptBiz;

    /**
     * 新增
     *
     * @param deptReq
     * @return RestResult
     */
    @PreAuth(value = "system:dept:save", description = "新增部门")
    @PostMapping("/api/system/dept/saveDept")
    public Result<Void> saveDept(@RequestBody @Validated(SaveGroup.class) DeptReq deptReq) {
        deptBiz.saveDept(deptReq);
        return Result.success();
    }

    /**
     * 通过Id修改
     *
     * @param deptReq
     * @return RestResult
     */
    @PreAuth(value = "system:dept:update", description = "修改部门")
    @PostMapping("/api/system/dept/updateDeptById")
    public Result<Void> updateDeptById(@RequestBody @Validated(UpdateGroup.class) DeptReq deptReq) {
        deptBiz.updateDeptById(deptReq);
        return Result.success();
    }

    /**
     * 通过Id查询部门
     *
     * @param deptId
     * @return RestResult
     */
    @PreAuth(value = "system:dept:select", description = "通过Id查询部门")
    @PostMapping("/api/system/dept/selectDeptById/{deptId}")
    public Result<DeptResp> selectDeptById(@PathVariable Long deptId) {
        return Result.data(deptBiz.selectDeptById(deptId));
    }

    /**
     * 通过id删除
     *
     * @param baseDeleteReq
     * @return RestResult
     */
    @PreAuth(value = "system:dept:delete", description = "删除部门")
    @PostMapping("/api/system/dept/deleteDept")
    public Result<Void> deleteDept(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseDeleteReq) {
        deptBiz.deleteDept(baseDeleteReq.getIds());
        return Result.success();
    }

    /**
     * 部门树
     *
     * @return
     */
    @PreAuth(value = "system:dept:select", description = "查询部门树")
    @PostMapping("/api/system/dept/selectDeptTree")
    public Result<List<DeptResp>> selectDeptTree() {
        return Result.data(deptBiz.selectDeptTree());
    }

    /**
     * 部门树
     *
     * @return
     */
    @PreAuth(value = "system:dept:select", description = "查询部门树")
    @PostMapping("/api/system/dept/selectAll")
    public Result<List<DeptResp>> selectAll(@RequestBody DeptReq deptReq) {
        return Result.data(deptBiz.selectAll(deptReq));
    }
}
